/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ventanas.Administracion;

import com.asistencia.DAO.factory.FactoryDAO;
import com.asistencia.model.Perfil;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author rfarias
 */
public class PerfilesMANT extends javax.swing.JFrame {
    
    private static Perfil pertemp;

    /**
     * Creates new form PerfilesMANT
     */
    public PerfilesMANT() {
        initComponents();
        RefrescaLista();
    }
    
     private void RefrescaLista()
    {
      List<Perfil> per = FactoryDAO.getInstance().getDataPer().findAll();
      DefaultTableModel dtm = new DefaultTableModel();
      dtm.setRowCount(0);
      dtm.addColumn("Nombre Perfil");
      dtm.addColumn("Alcance");
      dtm.addColumn("id");
                

      for(Perfil p: per)
         {
             dtm.addRow(new Object[] { p.getDescripcion(),p.getAlcance(), p.getId_perfil() }  );
         }
      tblPerfil.setModel(dtm);
      TableColumn columna = tblPerfil.getColumn("id");
      columna.setMaxWidth(0);
      columna.setWidth(0);
       
      tblPerfil.revalidate();
      tblPerfil.repaint();
      this.btnGuardar.setEnabled(false);
    }

     

     
     
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblPerfil = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txtNomPer = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txaAlcance = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SITA - Administración");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        setIconImage(new ImageIcon(getClass().getResource("/Ventanas/icono.png")).getImage());
        setResizable(false);
        setType(java.awt.Window.Type.POPUP);

        tblPerfil.setAutoCreateRowSorter(true);
        tblPerfil.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre de Perfil", "Alcance"
            }
        ));
        tblPerfil.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tblPerfil.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblPerfil.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblPerfilMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblPerfil);

        jLabel1.setText("Nombre de Perfil");

        jLabel2.setText("Alcance");

        btnGuardar.setText("Modificar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnSalir.setFont(new java.awt.Font("sansserif", 0, 9)); // NOI18N
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Mantenedor de Perfiles");

        txaAlcance.setColumns(20);
        txaAlcance.setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N
        txaAlcance.setLineWrap(true);
        txaAlcance.setRows(5);
        txaAlcance.setWrapStyleWord(true);
        jScrollPane2.setViewportView(txaAlcance);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSalir))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtNomPer, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNomPer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnGuardar)
                        .addComponent(btnSalir)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void tblPerfilMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblPerfilMouseClicked
        Number fila = this.tblPerfil.getSelectedRow();
             //JOptionPane.showMessageDialog(null, fila);
          if (fila==null)
          {

          }
          else
          {
            int perfilid = (Integer) tblPerfil.getValueAt(fila.intValue(),2);
            pertemp = (Perfil)FactoryDAO.getInstance().getDataPer().searchById(perfilid);
            this.txtNomPer.setText(pertemp.getDescripcion());
            this.txaAlcance.setText(pertemp.getAlcance());
            this.btnGuardar.setEnabled(true);
          }     
    }//GEN-LAST:event_tblPerfilMouseClicked

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        
        Perfil newper= new Perfil(this.txtNomPer.getText(),this.txaAlcance.getText());
        newper.setId_perfil(pertemp.getId_perfil());
        Perfil persav = (Perfil) FactoryDAO.getInstance().getDataPer().update(newper);
        if (persav!=null)
        {
            RefrescaLista();
            JOptionPane.showMessageDialog(rootPane, "Perfil "+persav.getDescripcion()+" modificado con exito", "Perfil Modificado", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            RefrescaLista();
            JOptionPane.showMessageDialog(rootPane, "Fallo al modificar perfil, intentelo nuevamente\n con parametros diferentes", "Fallo al Modificar", JOptionPane.ERROR_MESSAGE);
        }
        this.txtNomPer.setText(null);
        this.txaAlcance.setText(null);

    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        Perfil perno = (Perfil) FactoryDAO.getInstance().getDataPer().searchByDesc(this.txtNomPer.getText());
        if (perno!=null)
        {
            JOptionPane.showMessageDialog(rootPane, "El perfil que intenta ingresar ya existe.", "Fallo al Crear", JOptionPane.ERROR_MESSAGE);
            RefrescaLista();
        }
        else
        {
            Perfil newper= new Perfil(this.txtNomPer.getText(),this.txaAlcance.getText());
            Perfil persav = (Perfil) FactoryDAO.getInstance().getDataPer().insert(newper);
            if (persav!=null)
            {
                JOptionPane.showMessageDialog(rootPane, "Perfil "+persav.getDescripcion()+" creado con exito", "Perfil Creado", JOptionPane.INFORMATION_MESSAGE);
                RefrescaLista();
            }
        }
        this.txtNomPer.setText(null);
        this.txaAlcance.setText(null);
    }//GEN-LAST:event_btnNuevoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PerfilesMANT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PerfilesMANT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PerfilesMANT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PerfilesMANT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PerfilesMANT().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblPerfil;
    private javax.swing.JTextArea txaAlcance;
    private javax.swing.JTextField txtNomPer;
    // End of variables declaration//GEN-END:variables
}
